// pages/doctor/modify/modify.js
const jsonData = require("../jsondata.js");
var docotrservice = require("../../../apiservice/doctor/doctorservice");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        doctorItem: {},
        doctorObjItem: {},
        //选项集
        officeArray: jsonData.officeArray,
        officeIndex: 0,
        typeArray: jsonData.typeArray,
        typeIndex: 0,
        titleArray: jsonData.titleArray,
        titleIndex: 0,
        jobArray: jsonData.jobArray,
        jobIndex: 0,
        genderArray: jsonData.genderArray,
        operationType: jsonData.operationType,
        genderIndex: 0,
        intention: [{ id: 0, name: '无' }, { id: 1, name: '有' }],//合作意向
        intentionIndex: 0,
        hospitalName: '请选择',
        hospitalId: '',
        selectType: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //console.log(options);
        var item = JSON.parse(options.data);
        if (item.new_hospitalnameName != null) {
            this.setData({
                hospitalName: item.new_hospitalnameName,
                hospitalId: item.new_hospitalname
            });
        }
        //获取被选择的选项集
        this.getOptionsSelect(item)

        //获取被选择的标签
        this.getSelectItem(item);
        this.setData({
            doctorItem: item
        });
        if(item.new_type!=undefined){
          this.setData({typeIndex:this.GetTypeIndex(item.new_type)});
        }
    },
  GetTypeIndex: function (type) {
    let ret = -1;
    for (var i = 0; i < this.data.typeArray.length; i++) {
      if (type == this.data.typeArray[i].id) {
        ret = i;
        break;
      }
    }
    return ret;

  },
    //新增医生
    onSave: function () {
        var doctorobj = this.getDoctorObj();
        var msg = '';
        var isCreate = true;
        var flag = true;

      if (doctorobj.New_type == -1 && flag == true) {
        msg = '医生类型不能为空';
        isCreate = false;
        flag = false;
      }

        if (doctorobj.Lastname.replace(/\s+/g, '') == '' && flag == true) {
            msg = '姓名不能为空';
            isCreate = false;
            flag = false;
        }
        if (doctorobj.GenderCode == 0 && flag == true) {
            msg = '性别不能为空';
            isCreate = false;
            flag = false;
        }
		if (doctorobj.New_age != '') {
			if (doctorobj.New_age > 150) {
				msg = '请填写正确的年龄';
				isCreate = false;
				flag = false;
			}
		}

          if (flag) {
            if (doctorobj.MobilePhone.replace(/\s+/g, '') == '') {
              msg = '手机不能为空';
              isCreate = false;
              flag = false;
            } else {
              if (!(/^1[3456789]\d{9}$/.test(doctorobj.MobilePhone))) {
                msg = '手机号码不正确';
                isCreate = false;
                flag = false;
              }
            }
          }


        if (doctorobj.New_hospitalname == '' && flag == true) {
            msg = '医疗机构名称不能为空';
            isCreate = false;
            flag = false;
        }

        if(this.data.officeIndex==0 && flag==true){
          msg = '科室不能为空';
          isCreate = false;
          flag = false;
        }

        if (isCreate == false) {
            wx.showToast({
                title: msg,
                icon: 'none',
                duration: 2000
            })
        }
        else {
            console.log(doctorobj);
            this.modDoctor(doctorobj);
        }

    },
    /**
     * 文本框赋值
     * @param {} e 
     */
    onNameChange: function (e) {
        //console.log("onNameChange",e);
        this.setData({
            ['doctorItem.lastName']: e.detail
        })
    },
    onMobileChange: function (e) {
        //console.log("onMobileChange ",e);
        this.setData({
            ['doctorItem.mobilePhone']: e.detail
        })
    },
    onAgeChange: function (e) {
        //console.log("onAgeChange",e);
        this.setData({
            ['doctorItem.new_age']: e.detail
        })
    },
    onWeChatChange: function (e) {
        //console.log("onWeChatChange",e);
        this.setData({
            ['doctorItem.new_wechat']: e.detail
        })
    },
    /**
         * 选项集赋值
         * @param {*} e 
         */
    bindOfficeChange: function (e) {
        console.log("bindOfficeChange", e);
        this.setData({
            officeIndex: e.detail.value
        })
    },
    bindTypeChange: function (e) {
      this.setData({
        typeIndex: e.detail.value
      })
    },
    bindTitleChange: function (e) {
        //console.log("bindTitleChange",e);
        this.setData({
            titleIndex: e.detail.value
        })
    },
    bindJobChange: function (e) {
        //console.log("bindJobChange",e);
        this.setData({
            jobIndex: e.detail.value
        })
    },
    bindGenderChange: function (e) {
        //console.log("bindGenderChange",e);
        this.setData({
            genderIndex: e.detail.value
        })
    },
    bindIntentionChange: function (e) {
        this.setData({
            intentionIndex: e.detail.value
        });
    },
    onSelect: function (e) {
        // console.log(e);
        this.setData({
            selectType: e.detail
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    modDoctor: function (obj) {
        docotrservice.ModDoctor(obj).then(function (success) {
            console.log(success);
            wx.showToast({
                title: '修改成功',
                icon: 'success',
                duration: 2000
            });
            var doctorId = success.data.result;
            wx.navigateBack({
              url:'../detail/detail?doctorId='+doctorId
            });
        }).catch(function (ex) {
            console.log(ex);
            wx.showToast({
                title: '修改失败',
                icon: 'none',
                duration: 2000
            });
        });
    },
    toSelectHospital: function () {
        var that = this;
        wx.navigateTo({
            url: "../../institution/list/list",
            events: {
                openedHospitalPage: function (e) {
                    console.log(e.data)
                },
				passHospitalId:function(e){
					
				},
                HospitalEvent: function (data) {
                    console.log(data)
                    that.setData({
                        hospitalId: data.competitorId,
                        hospitalName: data.competitorName
                    });
                }
            },
            success: function (res) {
                res.eventChannel.emit('openedInstitutionPage', { data: 'choose' });
				res.eventChannel.emit('passHospitalId', {
					data: that.data.hospitalId
				})
            }
        })
    },
    getDoctorObj: function () {

        var doctorobj = this.data.doctorObjItem;
        var doctorViewObj = this.data.doctorItem;
        if(this.data.typeIndex==1 || this.data.typeIndex==2)
          doctorobj.New_type = this.data.typeArray[this.data.typeIndex].id;
        doctorobj.Contactid = doctorViewObj.contactid;
        doctorobj.Lastname = doctorViewObj.lastName;
        doctorobj.MobilePhone = doctorViewObj.mobilePhone;
        doctorobj.New_age = doctorViewObj.new_age;
        doctorobj.New_wechat = doctorViewObj.new_wechat;
        doctorobj.New_hospitalname = this.data.hospitalId;//医院
        if (this.data.genderIndex != 0) {
            doctorobj.GenderCode = this.data.genderArray[this.data.genderIndex].id;//性别
        }
        if (this.data.officeIndex != 0) {
            doctorobj.New_administrativeortechnicaloffices = this.data.officeArray[this.data.officeIndex].id;//科室
        }
        if (this.data.titleIndex != 0) {
            doctorobj.New_title = this.data.titleArray[this.data.titleIndex].id;//职称
        }
        if (this.data.jobIndex != 0) {
            doctorobj.New_hospitalduty = this.data.jobArray[this.data.jobIndex].id;//职务
            doctorobj.JobTitle= this.data.jobArray[this.data.jobIndex].name;
        }
        doctorobj.New_cataract = false;
        doctorobj.New_strabismus = false;
        doctorobj.New_glaucoma = false;
        doctorobj.New_lacrimalpassage = false;
        doctorobj.New_ametropia = false;
        doctorobj.New_fundustangent = false;
        var intention = false;
        if (this.data.intentionIndex == 1) {
            intention = true;
        }
        doctorobj.New_cooperativeintention = intention;//合作意向
        var selectType = this.data.selectType;
        if (selectType.length > 0) {
            for (var i = 0; i < selectType.length; i++) {
                if (selectType[i] == 1) {
                    doctorobj.New_cataract = true;
                }
                else if (selectType[i] == 2) {
                    doctorobj.New_strabismus = true;
                }
                else if (selectType[i] == 3) {
                    doctorobj.New_glaucoma = true;
                }
                else if (selectType[i] == 4) {
                    doctorobj.New_lacrimalpassage = true;
                }
                else if (selectType[i] == 5) {
                    doctorobj.New_ametropia = true;
                }
                else if (selectType[i] == 6) {
                    doctorobj.New_fundustangent = true;
                }
            }
        }
        return doctorobj
    },
    getSelectItem: function (doctorItem) {
        var selectType = [];
        if (doctorItem.new_cataract) {
            selectType.push("1");
        }
        if (doctorItem.new_strabismus) {
            selectType.push("2");
        }
        if (doctorItem.new_glaucoma) {
            selectType.push("3");
        }
        if (doctorItem.new_lacrimalpassage) {
            selectType.push("4");
        }
        if (doctorItem.new_ametropia) {
            selectType.push("5");
        }
        if (doctorItem.new_fundustangent) {
            selectType.push("6");
        }
        //console.log(selectType);
        this.setData({
            selectType: selectType
        })
    },
    getOptionsSelect(item) {
        if (item.new_title != null) {
            var titleIndex = this.getSelectIndex(item.new_title);
            this.setData({
                titleIndex: titleIndex
            });
        }

        if (item.new_administrativeortechnicaloffices != null) {
            var officeIndex = this.getSelectIndex(item.new_administrativeortechnicaloffices);
            this.setData({
                officeIndex: officeIndex
            });
        }

        if (item.new_hospitalduty != null) {
            var jobIndex = this.getSelectIndex(item.new_hospitalduty);
            this.setData({
                jobIndex: jobIndex
            });
        }

        if (item.new_cooperativeintention) {
            this.setData({
                intentionIndex: 1
            });
        }

        if (item.genderCode != null) {
            if (item.genderCode == 1) {
                this.setData({
                    genderIndex: 1
                });
            } else if (item.genderCode == 2) {
                this.setData({
                    genderIndex: 2
                });
            }
        } else {
            this.setData({
                genderIndex: 0
            });
        }
    },
    getSelectIndex: function (code) {
        var index = 0;
        if (code == "100000000") {
            index = 1;
        }
        else if (code == "100000001") {
            index = 2;
        }
        else if (code == "100000002") {
            index = 3;
        }
        else if (code == "100000003") {
            index = 4;
        }
        else if (code == "100000004") {
            index = 5;
        }
        else if (code == "100000005") {
            index = 7;
        }
        else if (code =="100000006")
          index=6;
        return index;
    }
})